;(function(hyh) {
    hyh.Form = Base.extend({
        msgType: 1, //消息提示形式 "弹框"，"tips"
        infoType: 1, //消息形式
        showAllErr: false, //提交时是否显示全部错误，默认 只显示一条  页面上所有表单设置一样
        attrName: "form-base",
        commErrId: "commErrId", //公共报错容器id
        errPostfix: "errspan",
        infoPostfix: "infospan",
        susinfoPostfix: "susspan",
        // errForm: {}, //错误表单
        priority: {}, //报错顺序
        emptyMsg: "不能为空", //空数据默认值
        elementArr:{},//表单元素id数组{formId:[],}
        formObject:{},//表单元素封装对象{formId:{formElemetId:obj}}
        // submitBefore:function(){},
        submitSuccess: function(url) {
            if (url) window.location.href = url;
        }, //提交成功
        submitFail: function() {}, //提交失败
        constructor: function(options) { //{formId:"",}
            if (!options || 　!options.formId) return {};
            var formEle = $("[" + this.attrName + "=" + options.formId + "]");

            this.formId = options.formId;
            if (!formEle.length) {
                return {};
            }

            this.msgType = options.msgType ? options.msgType : 1;
            this.buttonText = options.buttonText;
            this.submitText = options.submitText;
            this.showAllErr = options.showAllErr ? options.showAllErr : false;
            this.submitBefore = options.submitBefore ? options.submitBefore : null;
            this.submitSuccess = options.submitSuccess ? options.submitSuccess : this.submitSuccess;
            this.submitFail = options.submitFail ? options.submitFail : this.submitFail;
            //获取页面表单信息
            this[this.formId + "errForm"] = {};
            this[this.formId + "blurFun"] = options.blurFun ? options.blurFun : {};
            this[this.formId + "susFun"] = options.susFun ? options.susFun : {};
            this[this.formId + "errFun"] = options.errFun ? options.errFun : {};
            this[this.formId + "focusFun"] = options.focusFun ? options.focusFun : {};
            this[this.formId + "commErrId"] = options.commErrId ? options.commErrId : (this.formId + this.commErrId);
            this.formObject = {};
            this.formObject[this.formId] = {};
            this.elementArr = {};
            this.elementArr[this.formId] = [];
            var formEleArr;
            this.priority.length = 0;
            formEleArr = formEle.find("[" + this.attrName + "-config]");

            //创建单独表单
            for (var j = 0; j < formEleArr.length; j++) {
                this.createFormEach(formEleArr[j]);
            }

        },
        createFormEach: function(formEle) {
            //获取表单 config
            var formConfig = JSON.parse($(formEle).attr(this.attrName + "-config"));
            this.priority[formConfig.id] = formConfig.index ? formConfig.index : this.priority.length;
            //是否是必输项
            if (formConfig.required && !formConfig.initValue) {
                this[this.formId + "errForm"][formConfig.id] = {
                    status: false,
                    msg: formConfig.emptyMsg ? formConfig.emptyMsg : this.emptyMsg,
                    value: ""
                };
            }
            //创建每个表单元素
            if(formConfig.formType != "FormBtn"){
                this.elementArr[this.formId].push(formConfig.id)
                this.formObject[this.formId][formConfig.id] = new FormBase[formConfig.formType](formConfig.id, this, formConfig);
            }else{
                this.formObject[this.formId][formConfig.id] = new FormBase[formConfig.formType](formConfig.id, this, formConfig);
            }
        },
        /**
         * 显示错误
         * @param  {[type]} showAllErr 是否全部显示  true:全部，false:显示第一条
         * @return {[type]}            [description]
         */
        showErrors: function(formEleId, onlySelf) {
            this["msgType" + this.msgType](formEleId, onlySelf);
        },
        //清除错误信息
        clearErrors: function() {
            var errContianer1, errContainer2;
            //msgType:3
            errContainer2 = $("#" + this[this.formId + "commErrId"]);
            if (errContainer2.length) {
                // errContainer2.html("").addClass("hide")
                errContainer2.addClass("hide")
            }
            //msgType:1
            for (var key in this.priority) {
                errContianer1 = $("#" + key + this.errPostfix);
                // if (errContianer1) errContianer1.html("").addClass("hide");
                if (errContianer1) errContianer1.addClass("hide");
            }
        },

        getErrorContainer: function(formEleId) {
            var formEle = $("#" + formEleId + this.errPostfix);
            if (!formEle.length) {
                formEle = $("<span id= '" + formEleId + this.errPostfix + "'></span>");
                $("#" + formEleId).after(formEle);
            } else {
                formEle.removeClass("hide");
            }
            return formEle;
        },

        //处理错误提示顺序
        getFirstErrMsg: function() {
            var errForm = this[this.formId + "errForm"];
            var priorityArr = this.priority;
            var mostPr = "";
            for (var key in errForm) {
                !mostPr && (mostPr = key);
                if (priorityArr[key] < priorityArr[mostPr])
                    mostPr = key;
            }
            return mostPr;
        },
        //在每个表单的独立span中显示错误信息
        msgType1: function(formEleId, onlySelf) {
            var errContianer;
            if (formEleId) {
                if (this[this.formId + "errForm"][formEleId]) {
                    errContianer = this.getErrorContainer(formEleId); 
                    if(errContianer.find(".msgContent").length) errContianer = $(errContianer.find(".msgContent")[0]); 
                    errContianer.html(this[this.formId + "errForm"][formEleId].msg);
                }
            } else if (this.showAllErr && !onlySelf) {
                for (var key in this[this.formId + "errForm"]) {
                    errContianer = this.getErrorContainer(key);
                    if(errContianer.find(".msgContent").length) errContianer = $(errContianer.find(".msgContent")[0]); 
                    errContianer.html(this[this.formId + "errForm"][key].msg);
                }
            } else {
                var key = this.getFirstErrMsg();
   
                errContianer = this.getErrorContainer(key);
                if(errContianer.find(".msgContent").length) errContianer = $(errContianer.find(".msgContent")[0]); 
                errContianer.html(this[this.formId + "errForm"][key].msg);
            }
        },
        msgType2: function(formEleId) {
            // console.log("msgType2");
        },
        /**
         * 错误提示，再公共位置提示
         * 仅支持逐条展示
         * @return {[type]}        [description]
         */
        msgType3: function(formEleId) {
            var key = this.getFirstErrMsg();
            if (formEleId) {
                $("#" + this[this.formId + "commErrId"]).html( this[this.formId + "errForm"][formEleId].msg).removeClass("hide");
            } else if (this[this.formId + "errForm"][key]) {

                $("#" + this[this.formId + "commErrId"]).html(this[this.formId + "errForm"][key].msg).removeClass("hide");
            }
        },
        showInfo: function(formEleId, info) {
            this["infoType" + this.infoType](formEleId, info);
        },
        infoType1: function(formEleId, info) {
            var infoEle = $("#" + formEleId + this.infoPostfix);
            if (!infoEle.length) {
                infoEle = $('<span id="' + formEleId + this.infoPostfix + '"></span>');
                $("#" + formEleId).after(infoEle);
            } else {
                infoEle.removeClass("hide");
            }
            infoEle.html(info);
        },
        susInfo: function(formEleId, msg) {
            this["susinfoType" + this.infoType](formEleId, msg);
        },
        susinfoType1: function(formEleId, info) {
            var infoEle = $("#" + formEleId + this.susinfoPostfix);
            if (!infoEle.length) {
                infoEle = $('<span id="' + formEleId + this.susinfoPostfix + '"></span>');
                $("#" + formEleId).after(infoEle);
            } else {
                infoEle.removeClass("hide");
            }
            infoEle.html(info);
        },
        clearSusinfo: function(formEleId) {

            var infoEle = $("#" + formEleId + this.susinfoPostfix);
            var infoEle2 = $("#" + this[this.formId + "commErrId"]);
            infoEle.addClass("hide");
            infoEle2.addClass("hide");
        }

    });
})(window.hyh ? window.hyh : window.hyh={})
